package com.tvunetworks.device.extra.util;


import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;

/**
 * @author liam
 */
@Slf4j
public abstract class HttpRequestUtilV2 {
    private static final String X_FORWARDED_FOR      = "x-forwarded-for";
    private static final String COMMA_DELIMITER      = ",";
    private static final String PROXY_CLIENT_IP      = "Proxy-Client-IP";
    private static final String WL_PROXY_CLIENT_IP   = "WL-Proxy-Client-IP";
    private static final String X_REAL_IP            = "X-Real-IP";
    private static final String HTTP_X_FORWARDED_FOR = "HTTP_X_FORWARDED_FOR";
    private static final String HTTP_CLIENT_IP       = "HTTP_CLIENT_IP";

    /**
     * 获取http请求客户端的IP地址
     *
     * @param request 请求信息
     * @return client ip
     */
    public static String getClientIP(HttpServletRequest request) {
        String ip = request.getHeader(X_FORWARDED_FOR);
        if (StringUtils.hasText(ip)) {
            String[] ips = ip.split(COMMA_DELIMITER);
            for (String item : ips) {
                if (StringUtils.hasText(item)) {
                    String tempIp = item.trim();
                    if (IPUtils.isIP(tempIp)) {
                        return tempIp;
                    }
                }
            }
        }
        String reIp = "";
        if (IPUtils.isIP(ip = request.getHeader(PROXY_CLIENT_IP))) {
            reIp = ip.trim();
        } else if (IPUtils.isIP(ip = request.getHeader(WL_PROXY_CLIENT_IP))) {
            reIp = ip.trim();
        } else if (IPUtils.isIP(ip = request.getHeader(HTTP_CLIENT_IP))) {
            reIp = ip.trim();
        } else if (IPUtils.isIP(ip = request.getHeader(HTTP_X_FORWARDED_FOR))) {
            reIp = ip.trim();
        } else if (IPUtils.isIP(ip = request.getHeader(X_REAL_IP))) {
            reIp = ip.trim();
        } else if (IPUtils.isIP(ip = request.getRemoteAddr())) {
            reIp = ip.trim();
        } else if ("0:0:0:0:0:0:0:1".equalsIgnoreCase(ip)) {
            reIp = "0.0.0.0";
        }
        //logRequest(request, ip);
        return reIp;
    }


    /**
     * 获取公网IP
     *
     * @param request 请求信息
     * @return 公网ip
     */
    public static String getClientPublicIP(HttpServletRequest request) {
        String ip = request.getHeader(X_FORWARDED_FOR);
        if (StringUtils.hasText(ip)) {
            String[] ips = ip.trim().split(COMMA_DELIMITER);
            for (String item : ips) {
                if (StringUtils.hasText(item)) {
                    item = item.trim();
                    if (IPUtils.isPublicIP(item)) {
                        return item;
                    }
                }
            }
        }
        String reIp = "";
        if (IPUtils.isPublicIP(ip = request.getHeader(PROXY_CLIENT_IP))) {
            reIp = ip.trim();
        } else if (IPUtils.isPublicIP(ip = request.getHeader(WL_PROXY_CLIENT_IP))) {
            reIp = ip.trim();
        } else if (IPUtils.isPublicIP(ip = request.getHeader(HTTP_CLIENT_IP))) {
            reIp = ip.trim();
        } else if (IPUtils.isPublicIP(ip = request.getHeader(HTTP_X_FORWARDED_FOR))) {
            reIp = ip.trim();
        } else if (IPUtils.isPublicIP(ip = request.getHeader(X_REAL_IP))) {
            reIp = ip.trim();
        } else if (IPUtils.isPublicIP(ip = request.getRemoteAddr())) {
            reIp = ip.trim();
        }
        //logRequest(request, ip);
        return reIp;
    }


}
